package com.yuezhi.exception;

import com.yuezhi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: yuezhi-parent
 * @description:
 * @author: Mr.Liao
 * @create: 2020-12-16 13:58
 **/
@Slf4j
@ControllerAdvice
public class MyException {

    //全局异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        e.printStackTrace();
        return R.error().msg("执行了全局异常处理..");
    }

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e) {
        e.printStackTrace();
        return R.error().msg("执行了ArithmeticException异常处理..");
    }

    //自定义异常
    @ExceptionHandler(YueZhiException.class)
    @ResponseBody
    public R error(YueZhiException e) {
        log.error(e.getMessage());
        e.printStackTrace();

        return R.error().code(e.getCode()).msg(e.getMsg());
    }
}
